
// actionTypes集合，防止团队其它成员，声明同样的actionType
const actionTypes = [];

/**
 * 根据actionType和参数生成 actionCreator
 * @param {*} type 
 * @param  {...any} argNames 
 */
export function makeActionCreator(type) {
    // 该 actionType 已经存在了
    if (actionTypes.includes(type)) {
        throw new Error(`${type}已经存在了，请更换其它actionType`);
    } 

    // 存入集合中
    actionTypes.push(type);

    return function(payload) {
        const action = {
            creator: true,  // 是否使用makeActionCreator函数的标志
            type,
            payload
        };

        return action;
    }
}

/**
 * 对逆地理编码结果进行解析
 * @param {*} result 
 */
export function geocoderResolve(result){
    const { 
        addressComponent: { 
            poi_distance:poiDistance, 
            road_distance:roadDistance,
            poi, 
            road,
        }, 
        formatted_address:address = '', 
    } = result;

    let name = '未知地点';

    if(poiDistance && poi){
        if(Number(poiDistance) > roadDistance){
            if(roadDistance > 10){
                name = road + '附近';
            }else{
                name = road;
            }
        }else{
            if(Number(poiDistance) > 10){
                name = poi + '附近';
            }else{
                name = poi;
            }
        }
    } 

    return {
        name,
        address
    };
}